import STATUS from "@/enum/STATUS.js";

import {
  getPlatformList,
  getDepartmentList,
  getFunctionList,
  getRoleList
} from "@/api/authority.js";
import { getSkillList } from "@/api/global.js";
import { tileNestingData } from "@/assets/js/util.js";
import _ from "lodash";
//import { getUserInfo } from "@/api/user.js";

export default {
  /**
   * 获取用户信息
   * @param  {Object} context   [description]
   * @param  {Object} objParams 用于传入参数
   * @param  {Boolean} objParams.latest 是否重新请求数据，默认为false直接从state拿数据
   * @return {[type]}          Promise
   */
  // getUserInfo(context, objParams) {
  //   let latest = objParams ? objParams.latest : false;
  //   return new Promise((resolve, reject) => {
  //     if (context.state.userData && !latest) {
  //       resolve(context.state.userData);
  //       return;
  //     }
  //     getUserInfo().then(
  //       userResp => {
  //         let userRespData = userResp.data || {};
  //         if (userRespData.code === STATUS.success[0]) {
  //           localStorage.setItem("userData", JSON.stringify(userRespData.data));
  //           context.commit("setUserData", userRespData.data);
  //           resolve(userRespData.data);
  //         } else {
  //           reject(userRespData);
  //         }
  //       },
  //       () => {
  //         reject({
  //           code: STATUS.request_error[0],
  //           message: STATUS.request_error[1]
  //         });
  //       }
  //     );
  //   });
  // },
  /**
   * 获取平台类别
   */
  getPlatformList(context, objParams) {
    let latest = objParams ? objParams.latest : false;

    return new Promise((resolve, reject) => {
      if (context.state.allPlatformList && !latest) {
        resolve(context.state.allPlatformList);
        return;
      }
      getPlatformList({ params: { mask: 7, hierarchy: 1 } }).then(
        resp => {
          let respData = resp.data;

          if (respData.code === STATUS.success[0]) {
            let respResult = _.cloneDeep(respData.data);

            let respResultTile = tileNestingData(respResult);
            // respResultTile.map((item) => {
            //   item.id = "" + item.id;
            //   item.parentId = "" + item.parentId;
            //   return item;
            // });

            let platformList = []; // 发布列表
            let wantPlatformList = []; // 意向列表
            for (var i = 0, lenI = respResultTile.length; i < lenI; i++) {
              // 意向平台
              if (respResultTile[i].mask == 1) {
                wantPlatformList.push(respResultTile[i]);
              }

              // 发布平台
              if (respResultTile[i].mask == 2) {
                platformList.push(respResultTile[i]);
              }

              // 所有平台和都是意向和发布
              if (respResultTile[i].mask == 7 || respResultTile[i].mask == 3) {
                platformList.push(respResultTile[i]);
                wantPlatformList.push(respResultTile[i]);
              }
            }
            // 原始嵌套数据
            context.commit("setOrginNestingPlatformList", respData.data);
            // 所有平台列表
            localStorage.setItem(
              "allPlatformList",
              JSON.stringify(respResultTile)
            );
            context.commit("setAllPlatformList", respResultTile);

            // 发布平台列表
            localStorage.setItem("platformList", JSON.stringify(platformList));
            context.commit("setPlatformList", platformList);

            // 意向平台列表
            localStorage.setItem(
              "wantPlatformList",
              JSON.stringify(wantPlatformList)
            );
            context.commit("setWantPlatformList", wantPlatformList);
            resolve(respResultTile);
          } else {
            reject(respData);
          }
        },
        () => {
          reject({
            code: STATUS.request_error[0],
            message: STATUS.request_error[1]
          });
        }
      );
    });
  },
  /**
   * 获取才艺列表
   */
  getSkillList(context, objParams) {
    let latest = objParams ? objParams.latest : false;

    return new Promise((resolve, reject) => {
      if (context.state.skillList && !latest) {
        resolve(context.state.skillList);
        return;
      }
      getSkillList().then(
        resp => {
          let respData = resp.data;
          if (respData.code === STATUS.success[0]) {
            localStorage.setItem("skillList", JSON.stringify(respData.data));
            context.commit("setSkillList", respData.data);
            resolve(respData.data);
          } else {
            reject(respData);
          }
        },
        () => {
          reject({
            code: STATUS.request_error[0],
            message: STATUS.request_error[1]
          });
        }
      );
    });
  },
  /**
   * 获取部门列表
   */
  getDepartmentList(context, objParams) {
    let latest = objParams ? objParams.latest : false;
    return new Promise((resolve, reject) => {
      if (
        context.state.departmentList &&
        context.state.departmentList.length &&
        !latest
      ) {
        resolve(context.state.departmentList);
        return;
      }
      getDepartmentList().then(
        resp => {
          let respData = resp.data;
          if (respData.code === STATUS.success[0]) {
            // localStorage.setItem("departmentList", JSON.stringify(respData.data));
            context.commit("setDepartmentList", respData.data);
            resolve(respData.data);
            return;
          } else {
            reject(respData);
          }
        },
        () => {
          reject({
            code: STATUS.request_error[0],
            message: STATUS.request_error[1]
          });
        }
      );
    });
  },
  /**
   * 获取才艺列表
   */
  getFunctionList(context, objParams) {
    let latest = objParams ? objParams.latest : false;
    return new Promise((resolve, reject) => {
      if (
        context.state.functionList &&
        context.state.functionList.length &&
        !latest
      ) {
        resolve(context.state.functionList);
        return;
      }
      getFunctionList().then(
        resp => {
          let respData = resp.data;
          if (respData.code === STATUS.success[0]) {
            context.commit("setFunctionList", respData.data);
            resolve(respData.data);
            return;
          }
          reject(respData);
        },
        () => {
          reject({
            code: STATUS.request_error[0],
            message: STATUS.request_error[1]
          });
        }
      );
    });
  },
  /**
   * 获取角色列表
   */
  getRoleList(context, objParams) {
    let latest = objParams ? objParams.latest : false;
    return new Promise((resolve, reject) => {
      if (context.state.roleList && context.state.roleList.length && !latest) {
        resolve(context.state.roleList);
        return;
      }
      getRoleList().then(
        resp => {
          let respData = resp.data;
          if (respData.code === STATUS.success[0]) {
            context.commit("setRoleList", respData.data);
            resolve(respData.data);
            return;
          }
          reject(respData);
        },
        () => {
          reject({
            code: STATUS.request_error[0],
            message: STATUS.request_error[1]
          });
        }
      );
    });
  }
};
